package com.zhiwang.xiaoqu.mvp.model.sharepref

import android.annotation.SuppressLint
import android.content.Context
import android.preference.PreferenceManager

object PrefManager {

    lateinit var appContext: Context

    private const val KEY_TOKEN = "PrefKeyToken"

    private const val KEY_USERID = "PrefKeyUserId"

    fun init(context: Context) {

        appContext = context.applicationContext
    }

    @SuppressLint("ApplySharedPref")
    fun setToken(token: String) =

        PreferenceManager.getDefaultSharedPreferences(appContext)
            .edit()
            .putString(KEY_TOKEN, token)
            .commit()


    fun token(): String =
        PreferenceManager
            .getDefaultSharedPreferences(appContext)
            .getString(KEY_TOKEN, "")!!


    fun clearToken() = PreferenceManager
        .getDefaultSharedPreferences(appContext)
        .edit()
        .clear()
        .commit()

    fun setUserId(userid: String) =
        PreferenceManager.getDefaultSharedPreferences(appContext)
            .edit()
            .putString(KEY_USERID, userid)
            .commit()

    fun getUserId(): String =
        PreferenceManager
            .getDefaultSharedPreferences(appContext)
            .getString(KEY_USERID, "invalid userid-----")!!


    fun isLoginValid() = token().isEmpty()
}